//https://leetcode.cn/problems/binary-tree-preorder-traversal/description/
//二叉树前序遍历
//给你二叉树的根节点 root ，返回它节点值的 前序 遍历。

int TreeSize(struct TreeNode* root)
{
    return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}

void pretraversal(struct TreeNode* root, int* arr, int* pi)
{
    if(root == NULL)
    return;

    arr[*pi] = root->val;
    (*pi)++;
    pretraversal(root->left, arr, pi);
    pretraversal(root->right, arr, pi);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int* arr = (int*)malloc(sizeof(arr) * TreeSize(root));
    *returnSize = 0;
    pretraversal(root, arr, returnSize);
    return arr;
}